package org.example.acm;

import java.util.*;

public class ScoreSortSolution {
    private final static String TOTAL_SCORE = "zongfen";
    class Student {
        private String name;
        private int totalScore;
        private Map<String, Integer> scoreMap;
        Student(String name) {
            this.name = name;
            this.totalScore = 0;
            this.scoreMap = new HashMap<>();
        }

        void addScore(String subject, Integer score) {
            this.scoreMap.put(subject, score);
            this.totalScore = this.totalScore + score;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] firstLine = scanner.nextLine().split(" ");
        int studentCount = Integer.parseInt(firstLine[0]);
        int classCount = Integer.parseInt(firstLine[1]);
        String[] subjects = scanner.nextLine().split(" ");
        List<String[]> studentInfoList = new ArrayList<>();
        for (int i=0;i<studentCount;i++) {
            String[] studentInfo = scanner.nextLine().split(" ");
            studentInfoList.add(studentInfo);
        }
        String sortBy = scanner.nextLine();
        boolean flag = false;
        for (String subject: subjects) {
            if (subject.equals(sortBy)) {
                flag = true;
            }
        }
        sortBy = flag ? sortBy : TOTAL_SCORE;
        ScoreSortSolution solution = new ScoreSortSolution();
        List<Student> studentList = solution.buildStudentList(classCount, studentInfoList, subjects);
        List<Student> sortedStudentList = solution.sort(studentList, sortBy);
        for (int i=0;i<sortedStudentList.size();i++) {
            System.out.print(sortedStudentList.get(i).name);
            if (i != sortedStudentList.size()-1) {
                System.out.print(" ");
            }
        }
        scanner.close();
    }

    private List<Student> buildStudentList(int classCount, List<String[]> studentInfoList, String[] subjects) {
        List<Student> studentList = new ArrayList<>();
        for (int i=0;i<studentInfoList.size();i++) {
            Student student = new Student(studentInfoList.get(i)[0]);
            for (int j=1;j<=classCount;j++) {
                student.addScore(subjects[j-1], Integer.parseInt(studentInfoList.get(i)[j]));
            }
            studentList.add(student);
        }
        return studentList;
    }

    private List<Student> sort(List<Student> studentList, String sortBy) {
        if (sortBy.equals(TOTAL_SCORE)) {
            studentList.sort(new Comparator<Student>() {
                @Override
                public int compare(Student s1, Student s2) {
                    return s2.totalScore - s1.totalScore == 0 ? s1.name.compareTo(s2.name) : s2.totalScore - s1.totalScore;
                }
            });
        } else {
            studentList.sort(new Comparator<Student>() {
                @Override
                public int compare(Student s1, Student s2) {
                    return s2.scoreMap.get(sortBy) - s1.scoreMap.get(sortBy) == 0 ? s1.name.compareTo(s2.name) : s2.totalScore - s1.totalScore;
                }
            });
        }
        return studentList;
    }
}
